/*
 * TestDumpCCExpression.c
 *
 *  Created on: 2012/05/30
 *      Author: guanxiaoying
 */
#include "TestDump.h"
//typedef struct {
//	PSZ key;
//}TYPE_PROPERTY_KEY;
//static TYPE_PROPERTY_KEY stPropertysKey[]={
//		{"binary"},
//		{"delimiter"},
//		{"primary"},
//		{"postfix"},
//		{"unary"},
//		{"Initializer"},
//		{"@error"},
//		{"cast"},
//		{"parameter"},
//		{"parent"},
//		{"implicit"},
//		{"original"},
//		{"calculated"},
//		{"left"},
//		{"right"},
//		{"constant"},
//		{"address"},
//		{"variable"},
//		{"l_value"},
//		{"modifiable"},
//		{NULL}
//};
static VOID dumpExpression( AST astExp ){
	AST astCodeScp = CExpression.getCodeScope( astExp );
	U4 kind = CExpression.getOpKind( astExp );
	BOOL bConst = CExpression.isConstantExp( astExp );
	BOOL bAddrConst = CExpression.isAddressConstantExp( astExp );
	BOOL bLValue = CExpression.isLValue( astExp );
	BOOL bModify = CExpression.isModifiable( astExp );
	PSZ  property = "";

	sprintf( tempBuf, header_format, dumpGetStr(XML_RECORD), astExp );
	dumpOutputStream( tempBuf );

	dumpCodeScopeAsField( astCodeScp );

	if( CExpressionOpKind.isAdditiveOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isArgumentDelimiter(kind)){
		property = "delimiter";
	}else if( CExpressionOpKind.isAssignmentOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isBitwiseAndOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isBitwiseExclusiveOrOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isBitwiseInclusiveOrOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isCastOp(kind)){
		property = "cast";
	}else if( CExpressionOpKind.isCommaOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isConditionalOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isConstString(kind)){
		property = "primary";
	}else if( CExpressionOpKind.isConstantNumber(kind)){
		property = "primary";
	}else if( CExpressionOpKind.isDeclaration(kind)){
		property = "primary";
	}else if( CExpressionOpKind.isEqualityOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isIdentifier(kind)){
		property = "primary";
	}else if( CExpressionOpKind.isInitializer(kind)){
		property = "Initializer";
	}else if( CExpressionOpKind.isLogicalAndOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isLogicalOrOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isMultiplicativeOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isParentheseExp(kind)){
		property = "primary";
	}else if( CExpressionOpKind.isPostfixOp(kind)){
		property = "postfix";
	}else if( CExpressionOpKind.isRelationalOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isShiftOp(kind)){
		property = "binary";
	}else if( CExpressionOpKind.isStatement(kind)){
		property = "primary";
	}else if( CExpressionOpKind.isUnaryOp(kind)){
		property = "unary";
	}else{
		property = "@error";
	}
	sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), property );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, "%s", CCOutput.CExpressionOpKind->CcGetOpLabel(kind));
	dumpParameterStr( tempBuf );
	sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND));
	dumpOutputStream( tempBuf );

	if( CExpressionOpKind.isStatement(kind)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_STATEMENT), "parameter", CExpression.getPrimaryStatement( astExp) );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_STATEMENT) );
		dumpOutputStream( tempBuf );
	}else if( CExpressionOpKind.isConstString(kind) ){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_STRINGLITERAL), "parameter", CExpression.getPrimaryConstString( astExp) );
		dumpOutputStream( tempBuf );

	//	dumpOutputStream( dumpGetConstStr(CExpression.getPrimaryConstString( astExp)));

		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_STRINGLITERAL) );
		dumpOutputStream( tempBuf );
	}else if( CExpressionOpKind.isConstantNumber(kind) ){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_CONSTANT), "parameter", CExpression.getPrimaryConstValue( astExp) );
		dumpOutputStream( tempBuf );

		dumpParameterStr( dumpGetConstant(CExpression.getPrimaryConstValue( astExp)));

		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_CONSTANT) );
		dumpOutputStream( tempBuf );
	}else if( CExpressionOpKind.isDeclaration(kind)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_DECLARATION), "parameter", CExpression.getPrimaryDeclaration( astExp) );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_DECLARATION) );
		dumpOutputStream( tempBuf );
	}else if( CExpressionOpKind.isIdentifier(kind)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_SYMBOL), "parameter", CExpression.getPrimarySymbol( astExp) );
		dumpOutputStream( tempBuf );

		dumpParameterStr( CSymbol.getName(CExpression.getPrimarySymbol( astExp)) );

		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_SYMBOL) );
		dumpOutputStream( tempBuf );
	}else if( CExpressionOpKind.isInitializer(kind)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_INITIALIZER), "parameter", CExpression.getPrimaryInitializer( astExp) );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_INITIALIZER) );
		dumpOutputStream( tempBuf );
	}
	sprintf( tempBuf, sub_tab_header_format,
			dumpGetStr(XML_TYPE), "implicit", CExpression.getCastedTypeInfo( astExp) );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format,
			dumpGetStr(XML_TYPE) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format,
			dumpGetStr(XML_TYPE), "original", CExpression.getTypeInfo( astExp) );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format,
			dumpGetStr(XML_TYPE) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format,
			dumpGetStr(XML_CONSTANT), "calculated", CExpression.getCalcuteValue( astExp) );
	dumpOutputStream( tempBuf );
	dumpParameterStr( dumpGetConstant(CExpression.getCalcuteValue( astExp) ));
	sprintf( tempBuf, sub_tab_tail_format,
			dumpGetStr(XML_CONSTANT) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format,
			dumpGetStr(XML_EXPRESSION), "left", CExpression.getLeftNode( astExp) );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format,
			dumpGetStr(XML_EXPRESSION) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format,
			dumpGetStr(XML_EXPRESSION), "right", CExpression.getRightNode( astExp) );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format,
			dumpGetStr(XML_EXPRESSION) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_value_header_format,
			dumpGetStr(XML_FLAG), "constant_flag"  );
	dumpOutputStream( tempBuf );
	dumpParameterStr( bConst?"constant":bAddrConst?"address":"variable" );
	sprintf( tempBuf, sub_value_tail_format,
			dumpGetStr(XML_FLAG) );
	dumpOutputStream( tempBuf );

	if( bLValue ){
		sprintf( tempBuf, sub_value_header_format,
				dumpGetStr(XML_FLAG), "l-value"  );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "true" );
		sprintf( tempBuf, sub_value_tail_format,
				dumpGetStr(XML_FLAG) );
		dumpOutputStream( tempBuf );
	}
	if( bModify ){
		sprintf( tempBuf, sub_value_header_format,
				dumpGetStr(XML_FLAG), "modifiable"  );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "true" );

		sprintf( tempBuf, sub_value_tail_format,
				dumpGetStr(XML_FLAG) );
		dumpOutputStream( tempBuf );
	}

	if( CExpression.isParentDeclarator(astExp)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_DECLARATOR), "parent", CExpression.parent_getDeclarator( astExp) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_DECLARATOR) );
		dumpOutputStream( tempBuf );
	}else if( CExpression.isParentDesignation(astExp)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_DESIGNATION), "parent", CExpression.parent_getDesignation( astExp) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_DESIGNATION) );
		dumpOutputStream( tempBuf );
	}else if( CExpression.isParentExp(astExp)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_EXPRESSION), "parent", CExpression.parent_getExp( astExp) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_EXPRESSION) );
		dumpOutputStream( tempBuf );
	}else if( CExpression.isParentExpList(astExp)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_INITIALIZER), "parent", CExpression.parent_getExpList( astExp) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_INITIALIZER) );
		dumpOutputStream( tempBuf );
	}else if( CExpression.isParentStatement(astExp)){
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_STATEMENT), "parent", CExpression.parent_getStatement( astExp) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_STATEMENT) );
		dumpOutputStream( tempBuf );
	}else{
		sprintf( tempBuf, sub_tab_header_format,
				dumpGetStr(XML_ERROR), "parent", 0 );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format,
				dumpGetStr(XML_ERROR) );
		dumpOutputStream( tempBuf );
	}
	sprintf( tempBuf, tail_format,
			dumpGetStr(XML_RECORD) );
	dumpOutputStream( tempBuf );
}
VOID DumpExpression( PSZ szPath, PSZ postfix ){
	AST astExp;
	AST astMax;

	if( szPath == NULL ){
		return;
	}
	strcpy( file_buf, szPath );
	file_buf[strlen(szPath)] = '\0';
	strcat(file_buf, postfix );
	file_buf[strlen(szPath)+strlen(postfix)] = '\0';
	if( COEXIT_SUCCESS == dumpOutputStreamInit_file(file_buf)){
		astExp = MIN_ARRAY;
		astMax = CExpression.getMax();
		sprintf( tempBuf, top_header, dumpGetStr(XML_EXPRESSION) );
		dumpOutputStream( tempBuf );
		while( astExp <= astMax ){
			dumpExpression( astExp );
			astExp ++;
		}
		sprintf( tempBuf, top_end, dumpGetStr(XML_DECLARATOR) );
		dumpOutputStream( tempBuf );
		dumpOutputStreamEnd_file();
	}
}
